<template>
    <el-card class="box-card">
        <el-row>
            <p>这里是上传图片功能</p><br>
        </el-row>
        <el-row>
            <ef-from :model="form" label-width="80px">
                <el-upload class="avatar-uploader" action="/api/File/UploadFile" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </ef-from>
        </el-row>
        <el-row>
            <el-image style="width: 50px; height: 50px" :src="src" :preview-teleported="true" />
            <!-- <el-image style="vertical-align: middle;" :src="src" :zoom-rate="1" :initial-index="4" fit="scale-down" /> -->
        </el-row>
        <el-row>
            <!-- Form -->
            <el-button text @click="openSetting"> open a Form nested Dialog </el-button>
            <TestAdd :isShow="dialogFormVisible" @closeSetting="closeSetting" @saveSetting=saveSetting></TestAdd>
        </el-row>
    </el-card>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { UploadProps, ElMessage } from 'element-plus'
import store from '../../store';
import { FormatToken } from '../../tools';
import TestAdd from './TestAdd.vue'
const form = reactive({
    name: '',
    region: '',
    UserName: FormatToken(store().token)?.UserName,
    Password: "",
    Image: FormatToken(store().token)?.Image,
    uploadMode: "1"
})
const src = ref("http://rzj9n8iq3.hn-bkt.clouddn.com/aeb5b203a5ab43f8b5c7a86c98a47e4820230830190502236402.jpg")
const imageUrl = ref(form.Image)
const handleAvatarSuccess: UploadProps['onSuccess'] = (response, uploadFile) => {
    console.log(response)
    imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}

const dialogFormVisible = ref(false)
const openSetting = () => {
    dialogFormVisible.value = true
}
const closeSetting = () => {
    dialogFormVisible.value = false
}
const saveSetting = () => {
    dialogFormVisible.value = false
}
</script>
<style scoped>
.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}

.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>